 
import { request } from "../../request/index"
import { showModal,chooseAddress,showToast } from "../../utils/tools"
import regeneratorRuntime, { awrap } from '../../lib/runtime/runtime';

Page({ 
  /**
   * 页面的初始数据
   */
  data: {
    address:[],
    cart:[],  // 购物车数据 
    addNum:0,  // 总数
    addPrice:0,  // 总价
  },
 
  onShow(){
     let address =  wx.getStorageSync('address') || []
     // 获取本地缓存
     let cart = wx.getStorageSync('cart') || []  
     cart = cart.filter( v => v.checked ) 
     address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo 
    // 计算价格 数量 封装  
    let addNum = 0   
    let addPrice = 0  
    cart.forEach(v => {
       if(v.checked){ 
         addPrice += v.goods_price * v.num
         addNum += v.num
       } 
    }); 
     this.setData({
       address,
       cart,  
       addNum,
       addPrice
     }) 
  },

   
   // 结算
   handlePay(){
    let {address,cart,addPrice} = this.data  

    var token = wx.getStorageSync('token') || []
    console.log(token);
    if(token.length == 0){
      //  没有 token 需授权
      wx.navigateTo({
        url: '/pages/auth/index',
      })
    }else{
      //  有 token 接下来创建订单
    }
  }
})